{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analyzing Quantum Volume Routing Attempts\n",
    "This notebook analyzes the effect of using multiple routing attempts and picking the bestone for the Quantum Volume algorithmat a particular depth. For a given m = depth = number of qubits, plot the HOG for a range of routing attempts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configuration parameters. Feel free to mess with these!\n",
    "\n",
    "import cirq\n",
    "\n",
    "num_circuits = 10\n",
    "depth = 4\n",
    "routing_attempts = range(1, 100, 10) # [1, 6, 11, 16, 21, 26, 31]\n",
    "device = cirq.google.Bristlecone\n",
    "compiler = lambda circuit: cirq.google.optimized_for_xmon(\n",
    "    circuit=circuit,\n",
    "    new_device=device)\n",
    "\n",
    "print(f\"Configuration: depth {depth} with \"\n",
    "      f\"{num_circuits} circuits of routing attempts {routing_attempts}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run the Quantum Volume algorithm over the above parameters.\n",
    "\n",
    "import numpy as np\n",
    "from cirq.contrib import quantum_volume\n",
    "\n",
    "samplers = [\n",
    "        cirq.DensityMatrixSimulator(noise=cirq.ConstantQubitNoiseModel(\n",
    "        qubit_noise_gate=cirq.DepolarizingChannel(p=.005)))]\n",
    "\n",
    "results = []\n",
    "for r in routing_attempts:\n",
    "    print(f\"Running with {r} routing attempt(s)\")\n",
    "    results.append(quantum_volume.calculate_quantum_volume(num_circuits=num_circuits,\n",
    "                            depth=depth,\n",
    "                            num_qubits=depth,\n",
    "                            device=device,\n",
    "                            samplers=samplers,\n",
    "                            compiler=compiler,\n",
    "                            seed=None,\n",
    "                            routing_attempts=r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a chart that plots the HOG rate relative to the simulated error ratio.\n",
    "\n",
    "from matplotlib import pyplot as plt\n",
    "import statistics\n",
    "\n",
    "probs = []\n",
    "for idx, r in enumerate(routing_attempts):\n",
    "    result = results[idx]\n",
    "    probs.append(statistics.mean([res.sampler_result for res in result]))\n",
    "\n",
    "fig, axs = plt.subplots()\n",
    "axs.plot(routing_attempts, probs)\n",
    "\n",
    "# Line markers for asymptotic ideal heavy output probability and the ideal Heavy\n",
    "# Output Generation threshold.\n",
    "axs.axhline((1 + np.log(2)) / 2,\n",
    "            color='tab:green',\n",
    "            label='Asymptotic ideal',\n",
    "            linestyle='dashed')\n",
    "axs.axhline(2 / 3, label='HOG threshold', color='k', linestyle='dotted')\n",
    "axs.set_ybound(0.4, 1)\n",
    "axs.set_xlabel(\"Number of routing attempts\")\n",
    "axs.set_ylabel(\"est. heavy output probability\")\n",
    "fig.suptitle(f'HOG probability by number of routing attempts for d={depth}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
